/*
 * Lista.cpp
 *
 *  Created on: 23/08/2012
 *      Author: vagner
 */




#include "Lista.h"
#include <iostream>

Lista::Lista(){
	this->first = 0;
	this->last = 0;
	this->numItens = 0;
}

int Lista::addItem(Item i){
	int rc;

	if(numItens == 0)//Lista Vazia
	{
		first = new Nodo;
		if(first == NULL){
			rc = 0;
		}else{
			first->item = i;
			numItens++;
			last = first;
			last->next = NULL;
			rc = 1;
		}
	}else{
		last->next = new Nodo;
		if(last->next == NULL){
			rc = 0;
		}else{
			last = last->next;
			last->next = NULL;
			numItens++;
			rc = 1;
		}
	}
	return rc;
}

void Lista::deleteAllItens(){


	pNodo aux;

	while(first != NULL){
		aux = first;
		first = first->next;
		delete aux;
	}
	last = first;
}

int Lista::deleteItem(Item i){
	int rc = 0;
	pNodo it = 0;
	pNodo aux = 0;

	if(numItens == 0){
		rc = 0;
		return rc;
	}else if(first->item == i){
			first = first->next;
			delete it;
			numItens--;
			rc = 1;
	}else{
		for(it = first->next; it != NULL; it = it->next){
			if(it->next->item == i){
				aux = it->next;
				it->next = aux->next;
				delete aux;
				if(it->next == last){
					last = it;
				}
				rc = 1;
			}
		}

		if(it == NULL){
			rc = 0;
		}
	}
	return rc;
}

int Lista::findItem(Item i) const{
	pNodo it = 0;

	for(it=first;it !=NULL;it = it->next){

		if(it->item == i){
			return 1;
		}

	}
	return 0;

}
